// pages/scan/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
		
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var userInfo = wx.getStorageSync('userInfo');
    if (!userInfo || userInfo == '') {
        wx.navigateTo({
          url: '/pages/index/login/index',
        }); return
		}
		this.setData({
			code: options.code,
		});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

	// 扫码添加
	toAdd: function (e) {
		var that = this;
		wx.scanCode({
			success: function (res) {
				console.log(res.result);
				var code = that.getParamByUrl({url: res.result, key: 'code'});
				// console.log(code); return
				wx.request({
					method: 'POST',
					url: app.globalData.domainXcx + '/tmp/exist',
					data: {code: code},
					timeout: 10000, //接口超时10S
					success: function (res) {
						res = res.data;
						console.log(res); //return
						if (res.code < 0) {
							wx.showToast({
								title: res.msg, icon: 'none'
							}); return
						}
						wx.navigateTo({
							url: '/pages/scan/scene/index?code=' + e,
						}); return
					},
					fail: function () {
						console.log('error');
					}
				});
			}
		});
	},

	// 扫码修改
	toEdit: function (e) {
		var that = this;
		wx.scanCode({
			success: function (res) {
				console.log(res.result);
				var code = that.getParamByUrl({url: res.result, key: 'code'});
				console.log(code); //return
				wx.request({
					method: 'POST',
					url: app.globalData.domainXcx + '/tmp/exist',
					data: {code: code},
					timeout: 10000, //接口超时10S
					success: function (res) {
						res = res.data;
						console.log(res);
						if (res.code != -2) {
							wx.showToast({
								title: '没有被使用', icon: 'none'
							}); return
						}
						wx.navigateTo({
							url: '/pages/index/tmpOne/index?code=' + code,
						}); return
					},
					fail: function () {
						console.log('error');
					}
				});
			}
		});
	},

	// 编号查询
	toQuery: function (e) {
		wx.navigateTo({
			url: '/pages/scan/serial/index',
		});
	},

	// 从URL中获取参数
	getParamByUrl: function (e) {
			var httpUrls = e.url.split('?');
			console.log(app.globalData.domain);
			console.log(httpUrls);
			if (httpUrls[0] != app.globalData.domain 
					&& httpUrls[0] != app.globalData.domainH5) {
					wx.showToast({
							title: '非法二维码', icon: 'none'
					}); return
			}
			if (httpUrls[1] == '') {
					wx.showToast({
							title: '没有参数', icon: 'none'
					}); return
			}
			// console.log(httpUrls[1]);
			var params = httpUrls[1].split('&');
			var i = 0, kvs = '', val = '';
			for (; i < params.length; i ++) {
					kvs = params[i].split('=');
					if (kvs[0] == e.key) {
							val = kvs[1];
					}
			}
			return val;
	},
})